﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4SerialInput : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4SerialInput()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F4RobotOrderDetail.DoubleClick += F5Plan_DoubleClick;
            this.F4RobotOrderDetail.PopupMenuShowing += F5Plan_PopupMenuShowing;
            this.f4SerialContextMenu.ItemClicked += f5PlanContextMenu_ItemClicked;
        }

        void f5PlanContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4SerialContextMenu.Hide();
            DataRow row = F4RobotOrderDetail.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {                
                case "addSelection":                    
                    frm_Add_F4SerialInput add_Form = new frm_Add_F4SerialInput();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "editSelection":
                    /*
                    if (!string.IsNullOrEmpty(row[7].ToString()))
                    {
                        MessageBox.Show("This plan has been Finished!");
                    }
                    else
                    {
                        frm_Edit_F5Plan edit_Form = new frm_Edit_F5Plan();
                        edit_Form.planid = Int32.Parse(row[0].ToString());
                        if (edit_Form.LoadData())
                        {
                            edit_Form.Show();
                        }
                    } */
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F5Plan_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4SerialContextMenu.Show(MousePosition);
        }

        private void F5Plan_DoubleClick(object sender, EventArgs e)
        {            
            /*
            DataRow row = F4RobotOrderDetail.GetFocusedDataRow();            
            if (!string.IsNullOrEmpty(row[7].ToString()))
            {
                MessageBox.Show("This plan has been Finished!");
            }
            else
            {
                frm_Edit_F5Plan editForm = new frm_Edit_F5Plan();
                editForm.planid = Int32.Parse(row[0].ToString()); ;
                if (editForm.LoadData())
                {
                    editForm.Show();
                }
            }  */
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [231_TimePlan].TimePlanNo, [231_TimePlan].PlanNo, [231_TimePlan].CompID,"
                + " [231_TimePlan].Quantity, [231_TimePlan].FromSerialNo, [231_TimePlan].ToSerialNo,"
                + " [231_TimePlan].DeadLine, [222_Customer].FullName, [231_TimePlan].Remark"
                + " FROM [231_TimePlan] LEFT OUTER JOIN"
                + " [222_Customer] ON [222_Customer].CustomerID=[231_TimePlan].CustomerID"
                + " ORDER BY [231_TimePlan].TimePlanNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4RobotOrderDetail.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("TimePlanNo", typeof(string));
                newTable.Columns.Add("PlanNo", typeof(string));
                newTable.Columns.Add("CompID", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("FromSerialNo", typeof(string));
                newTable.Columns.Add("ToSerialNo", typeof(string));
                newTable.Columns.Add("DeadLine", typeof(DateTime));
                newTable.Columns.Add("Customer", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                gridF4RobotOrderDetail.DataSource = newTable;
            }
            F4RobotOrderDetail.Columns[0].Caption = "Time Plan No";
            F4RobotOrderDetail.Columns[1].Caption = "Plan No";            
            F4RobotOrderDetail.Columns[2].Caption = "Comp ID";
            F4RobotOrderDetail.Columns[3].Caption = "Quantity";
            F4RobotOrderDetail.Columns[4].Caption = "From Serial No";
            F4RobotOrderDetail.Columns[5].Caption = "To Serial No";
            F4RobotOrderDetail.Columns[6].Caption = "Dead Line";
            F4RobotOrderDetail.Columns[7].Caption = "Customer";
            F4RobotOrderDetail.Columns[8].Caption = "Remark";
            F4RobotOrderDetail.BestFitColumns();
        }

        private void poAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F4SerialInput add_Form = new frm_Add_F4SerialInput();
            add_Form.ShowDialog();
            //LoadData();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
